<?php
namespace app\index\controller;
use think\Db;
class Base extends \think\Controller
{   
    public $userinfo;
    public $url;
    
    public function __construct(){
        parent::__construct();
        $this->url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->_checkLogin();
        $this->_appConfig();
    }
    /**
     * 验证登录
     */
    private function _checkLogin(){
        if(com_controller()=='publics')
            return;
        
        $user = new \app\index\model\User();
        if( !$user->isLogin() ){
            if( is_weixin() ){
                $wx    = new \app\common\model\Wx();
                if( !isset($_GET['code']) ){
                    $url = $wx->CreateOauthUrlForCode($this->url);
                    exit("<script language='javascript'>window.location='".$url."'</script>");
                }else{
                    $uuid    = $wx->GetOpenidFromMp( $_GET['code'] );
                    $muid    = input('get.muid');
                    $user->wxLogin( $uuid, $muid );
                }
            }else{
                //die('请在微信中打开...');
                session('user_id',6);r(r(url("index/index")));
                //r(url("Publics/logOut"));
            }
        }
        $this->_global();
    }
    
    /**
     * 如果登录，将用户信息写入全局变量
     */
    private function _global(){
        $id = session("user_id");
        if( $id ){
            $this->userinfo = Db::name('user')->find($id);
            $this->assign('userinfo', $this->userinfo);
            \my\G::set('userinfo', $this->userinfo);
        }
    }
    
    /**
     * 项目配置赋值
     */
    private function _appConfig(){
        $con    = c('app');
        foreach($con as $k => $v){
            $this->assign('app_'.$k, $v);
        }
        
        $sys = new \app\common\model\Sys();
        $rs = $sys->get(['title', 'keyword', 'description', 'right']);
        $this->assign("sys", $rs);

        $nav5 = Db::name('nav')->where('cat_id',5)->where('status',0)->order("sort desc,id desc")->select();
        $this->assign("nav5", $nav5);
        
        $nav6 = Db::name('nav')->where('cat_id',6)->where('status',0)->order("sort desc,id desc")->select();
        $this->assign("nav6", $nav6);
    }
}